// DEFINE 'QUESTION' OBJECT
  	function newQuestion(question,answer1,answer2,answer3,correctAnswer,explanation) {
  	  this.question = question;
  	  this.answer1 = answer1;
	  this.answer2 = answer2;
	  this.answer3 = answer3;
  	  this.correctAnswer = correctAnswer;
	  this.explanation = explanation;
  	};

// CREATE ARRAY OF QUESTION OBJECTS
  var questionArray = new Array(
  	 question = new newQuestion(
  	  	"Who is Will's sister?",
  	  	"Peter",
  	  	"Katy",
  	  	"Debby"
  	  	,2,
		"Yes, Katy is Will's sister!"
		),	
	 question = new newQuestion(
  	  	"Who is Will's uncle?",
  	  	"Peter",
  	  	"Robert",
  	  	"Tom"
  	  	,3,
		"Yes, Tom is Will's uncle!"
		),
	question = new newQuestion(
  	  	"What is the capital of Paris?",
  	  	"Paris, France",
  	  	"Paris, Texas",
  	  	"Wolverhampton"
  	  	,1,
		"The capital of France is indeed Paris!"
		),
	question = new newQuestion(
  	  	"In what year was M.R. James born?",
  	  	"1852",
  	  	"1862",
  	  	"1892"
  	  	,2,
		"M.R. James was born on 1st August 1862 in the village of Goodnestone, Kent."
		),
	question = new newQuestion(
  	  	"In which European city is 'Number 13' set?",
  	  	"Stockholm",
  	  	"Rotterdam",
  	  	"Viborg"
  	  	,3,
		"The story takes place in Viburg in northern Denmark."
		)
	);
//data-icon='star' 
function compileQuestion(questionNumber){
	var thisQuestion = "";
	thisQuestion = thisQuestion + "<h2>" + questionArray[questionNumber].question + "</h2><div id='result'></div>";
	thisQuestion = thisQuestion + "<a href='#' data-role='button' data-icon='star' class='answer-button qnum1' >" + questionArray[questionNumber].answer1 + "</a>";
	thisQuestion = thisQuestion + "<a href='#' data-role='button' data-icon='star' class='answer-button qnum2' >" + questionArray[questionNumber].answer2 + "</a>";
	thisQuestion = thisQuestion + "<a href='#' data-role='button' data-icon='star' class='answer-button qnum3' >" + questionArray[questionNumber].answer3 + "</a>";
	return thisQuestion;
}
	
function checkAnswer(thisQuestionNumber, selectedAnswerNumber) {
	$("a").each(function(index) {
		if ((index + 1) == questionArray[currentQuestionNumber].correctAnswer) {
				$("span.ui-btn-inner", this).css({'background-color': 'green'});
				//$("span.ui-icon", this).removeClass('ui-icon-star').addClass('ui-icon-check');
		} else {
				$("span.ui-btn-inner", this).css({'background-color': 'red'});
		}
	});
	if (selectedAnswerNumber == questionArray[currentQuestionNumber].correctAnswer) {
		$("a:eq(" + (selectedAnswerNumber - 1) + ")").buttonMarkup({ icon: "check" });
		$("#result").append("<p>Correct Answer!</p>").css({"color": "#009900"});
	} else { 
		$("a:eq(" + (selectedAnswerNumber - 1) + ")").buttonMarkup({ icon: "delete" });
		$("#result").append("<p>Wrong Answer!</p>").css({"color": "#cc0000"});
	};
	$("#current-question").append("<a href='#' data-role='button' data-icon='arrow-r' data-mini='true' class='next-button' style='margin-top:20px;' >NEXT</a>");
	$("content").trigger('create');
	$(".next-button").click(function() {
		location.reload(false);
	});
};	

var numberOfQuestions = questionArray.length;
var currentQuestionNumber = Math.floor((Math.random()*numberOfQuestions));
var selectedAnswerNumber = 0;

$(function() {
	var thisQuestion = compileQuestion(currentQuestionNumber);
	$("#current-question").append(thisQuestion);
	$("content").trigger('create');
	$(".answer-button").click(function() { 
		if ( $(this).hasClass('qnum1') ) {selectedAnswerNumber = 1};
		if ( $(this).hasClass('qnum2') ) {selectedAnswerNumber = 2};
		if ( $(this).hasClass('qnum3') ) {selectedAnswerNumber = 3};
		checkAnswer(currentQuestionNumber, selectedAnswerNumber); 
	});
});